package com.lzxuni.modules.weather.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.HumpUtils;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.cropinformation.entity.TblPlantingInformation;
import com.lzxuni.modules.cropinformation.service.TblPlantingInformationService;
import com.lzxuni.modules.weather.entity.WeatherInfo;
import com.lzxuni.modules.weather.entity.WeatherLimit;
import com.lzxuni.modules.weather.entity.WeatherLimitDetail;
import com.lzxuni.modules.weather.mapper.WeatherInfoForecastHourMapper;
import com.lzxuni.modules.weather.service.WeatherInfoForecastHourService;
import com.lzxuni.modules.weather.service.WeatherLimitDetailService;
import com.lzxuni.modules.weather.service.WeatherLimitService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 天气信息_预测小时天气管理 服务实现类
 * </p>
 *
 * @author LaoNa
 * @since 2020-11-27
 */
@Service
public class WeatherInfoForecastHourServiceImpl extends ServiceImpl<WeatherInfoForecastHourMapper, WeatherInfo> implements WeatherInfoForecastHourService {


    //作物种植信息
    @Autowired
    private TblPlantingInformationService tblPlantingInformationService;

    //预警信息
    @Autowired
    private WeatherLimitService weatherLimitService;

    //预警信息明细
    @Autowired
    private WeatherLimitDetailService weatherLimitDetailService;
    @Override
    public PageInfo<WeatherInfo> queryPage(PageParameter pageParameter, WeatherInfo weatherInfo) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    HumpUtils.humpToLine(pageParameter.getSidx()) + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<WeatherInfo> weatherInfoList = baseMapper.queryList(weatherInfo);
        //            log.error("天气信息长度："+weatherInfoList.size());
        //获取年份
        String year="";
        if(StringUtils.isNotBlank(weatherInfo.getCreateDate())){
            year=DateFormatUtils.format(weatherInfo.getCreateDate(),"yyyy");
        }else{
            year=DateFormatUtils.format(new Date(),"yyyy");
        }
        //--预警信息 开始
        //根据地区和年 查询当年种植的农作物
        TblPlantingInformation tblPlantingInformation=tblPlantingInformationService.getOne(new QueryWrapper<TblPlantingInformation>().eq("planting_year",year).eq("land_Id",weatherInfo.getParentId()));
        if(tblPlantingInformation!=null&&StringUtils.isNotBlank(tblPlantingInformation.getCropModela())){
            //--查询出完整的预警信息 开始
            //查询预警信息
            List<WeatherLimit> limitList= weatherLimitService.list(new QueryWrapper<WeatherLimit>().eq("crop_id",tblPlantingInformation.getCropModela()));
            if(limitList!=null&&limitList.size()>0){
                for(WeatherLimit weatherLimit:limitList){
                    //查询预警明细信息
                    List<WeatherLimitDetail> weatherLimitDetailList=weatherLimitDetailService.list(new QueryWrapper<WeatherLimitDetail>().eq("limit_id",weatherLimit.getLimitId()));
                    if(weatherLimitDetailList!=null&&weatherLimitDetailList.size()>0){
                        weatherLimit.setWeatherLimitDetails(weatherLimitDetailList);
                    }
                }
            }
            //--查询出完整的预警信息 结束
            //--比较预警信息 开始
            if (weatherInfoList != null && weatherInfoList.size() > 0) {
                //--遍历天气信息 开始
                for (WeatherInfo weatherInfoItem : weatherInfoList) {
                    weatherInfoItem.setDisastersName("");
                    if (limitList != null && limitList.size() > 0) {
                        String msg="";
                        //--遍历预警主信息 开始
                        for (WeatherLimit weatherLimit : limitList) {
                            List<WeatherLimitDetail> weatherLimitDetailList = weatherLimit.getWeatherLimitDetails();
                            if (weatherLimitDetailList != null && weatherLimitDetailList.size() > 0) {
                                int mark = 0;
                                //--遍历比较预警详细表 开始
                                for (int i = 0; i < weatherLimitDetailList.size(); i++) {
                                    WeatherLimitDetail weatherLimitDetail = weatherLimitDetailList.get(i);
                                    BigDecimal num = new BigDecimal("0");
                                    if (weatherLimitDetail.getDetailType().equals("evaporation_capacity")) {  //蒸发量
                                        num = weatherInfoItem.getEvaporationCapacity();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("earth_temperature")) {  //地温
                                        num = weatherInfoItem.getEarthTemperature();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("light_intensity")) {  //光照强度
                                        num = weatherInfoItem.getLightIntensity();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("air_temperature")) {  //空气温度
                                        num = weatherInfoItem.getAirTemperature();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("wind_speed")) {  //风速
                                        num = weatherInfoItem.getWindSpeed();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("wind_direction")) {  //风向
                                        num = weatherInfoItem.getWindDirection();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("rainfall")) {  //降雨量
                                        num = weatherInfoItem.getRainfall();
                                    }
                                    if (weatherLimitDetail.getDetailType().equals("air_humidity")) {  //空气湿度
                                        num = weatherInfoItem.getAirHumidity();
                                    }
                                    if (num != null && weatherLimitDetail.getDetailBegin() != null && weatherLimitDetail.getDetailEnd() != null) {
                                        if (num.compareTo(weatherLimitDetail.getDetailBegin()) > -1 && num.compareTo(weatherLimitDetail.getDetailEnd()) < 1) {
                                            mark++;
                                        }
                                    }
                                }
                                //--遍历比较预警详细表 结束
                                if (mark == weatherLimitDetailList.size()) {
                                    msg+=weatherLimit.getLimitName()+",";
                                }

                            }
                        }
                        //--遍历预警主信息 结束
                        if(msg.length()>0){
                            weatherInfoItem.setDisastersName(msg.substring(0,msg.length()-1));
                        }
                    }
                }
                //--遍历天气信息 结束
            }
            //--比较预警信息 结束
        }
        //--预警信息 结束
        PageInfo<WeatherInfo> pageInfo = new PageInfo<>(weatherInfoList);
        return pageInfo;
    }

    @Override
    public List<WeatherInfo> queryList(WeatherInfo weatherInfo) {
        List<WeatherInfo> weatherInfoList = baseMapper.queryList(weatherInfo);
        return weatherInfoList;
    }
    @Override
    public Workbook exportExcel(WeatherInfo weatherInfo) throws IOException {
        PageHelper.orderBy("create_date" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        PageParameter pageParameter=new PageParameter();
        pageParameter.setPage(1);
        pageParameter.setRows(100000);
        List<WeatherInfo> weatherInfoList =this.queryPage(pageParameter, weatherInfo).getList();
        List<String> list0 = new ArrayList<>();
        list0.add("所属地块");
        list0.add("当前时间");

        list0.add("年份");
        list0.add("时间");
        list0.add("空气温度");
        list0.add("空气湿度");

        list0.add("光照强度");
        list0.add("地温");
        list0.add("风速");
        list0.add("风向");
        list0.add("降雨量");

        list0.add("蒸发量");
        list0.add("预警信息");

        listData1.add(list0);
        if (weatherInfoList != null && weatherInfoList.size() > 0) {
            for (int i = 0; i < weatherInfoList.size(); i++) {
                WeatherInfo weatherInfo1 = weatherInfoList.get(i);
                List<String> list = new ArrayList<>();

                list.add(weatherInfo1.getLandNum());
                list.add(weatherInfo1.getBeginDate());
                list.add(weatherInfo1.getInfoYear());
                list.add(DateFormatUtils.format(weatherInfo1.getCreateDate(),"yyyy-MM-dd hh:mm:ss"));
                list.add(weatherInfo1.getAirHumidity().toString());
                list.add(weatherInfo1.getAirTemperature().toString());

                list.add(weatherInfo1.getLightIntensity().toString());
                list.add(weatherInfo1.getEarthTemperature().toString());
                list.add(weatherInfo1.getWindSpeed().toString());
                list.add(weatherInfo1.getWindDirection().toString());
                list.add(weatherInfo1.getRainfall().toString());

                list.add(weatherInfo1.getEvaporationCapacity().toString());
                list.add(weatherInfo1.getDisastersName());

                listData1.add(list);
            }
        }
        dataMap.put("预测12小时天气列表", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "预测12小时天气列表");
        return wb;
    }

}
